Does Each Call to SP.ClientContext.get_current() Return the Same Instance?

Comments 0

Share to social media

I was trying to figure this out for some common code that we are putting together so I ran the following test:

function TestReferences() {
    var contextA = SP.ClientContext.get_current();
    var contextB = SP.ClientContext.get_current();

    contextA.test = 5;
    alert(contextB.test);         //Displays 5
    alert(contextA == contextB);  //Results in True
    alert(contextA === contextB); //Results in True
}

The result was that each call to SP.ClientContext.get_current() returns the same instance each time, which is what I was hoping based on the name of the function.  This is also true for the get_web() call off the client context object.  If you need to return a different context then you can still spin up your own, but if you want to reuse the same context between pieces of code then SP.ClientContext.get_current() allows you to do that without having create your own variable to share out the instance.

Load comments

About the author

Damon Armstrong

See Profile

Damon Armstrong is a consultant with SystemwarePS in Dallas, Texas. He is also a blogger and author of Pro ASP.NET 2.0 Website Programming and SharePoint 2013 Essentials for Developers. He specializes in the Microsoft stack with a focus on web technologies like MVC, ASP.NET, JavaScript, and SharePoint. When not staying up all night coding, he can be found watching a bunch of kids, studying Biblical topics, playing golf, or recovering from staying up all night coding.